package com.geovis.emergency.spd.biz.system.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.basic.service.IBasicDutyOrgService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDutyOrgService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillService;
import com.geovis.emergency.spd.biz.pc.service.IPcPlanDutyOrgService;
import com.geovis.emergency.spd.biz.system.mapper.SystemRoleMapper;
import com.geovis.emergency.spd.biz.system.service.ISystemRoleResourcesService;
import com.geovis.emergency.spd.biz.system.service.ISystemRoleService;
import com.geovis.emergency.spd.biz.system.service.ISystemUserRoleService;
import com.geovis.emergency.spd.entity.basic.entity.BasicDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcPlanDutyOrg;
import com.geovis.emergency.spd.entity.system.constant.SystemRoleConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemRole;
import com.geovis.emergency.spd.entity.system.entity.SystemRoleResources;
import com.geovis.emergency.spd.entity.system.entity.SystemUserRole;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemRoleAssignResourceDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 王响
 * @since 2020-07-15
 */
@Service
@AllArgsConstructor
@Slf4j
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRole> implements ISystemRoleService {

    private final ISystemRoleResourcesService systemRoleResourcesService;

    private final ISystemUserRoleService systemUserRoleService;

    private final IBasicDutyOrgService basicDutyOrgService;

    private final IPcPlanDutyOrgService pcPlanDutyOrgService;

    private final IPcDrillDutyOrgService pcDrillDutyOrgService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void removeByIdList(Set<String> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        // 1、删除角色资源中间表
        systemRoleResourcesService.remove(Wrappers.lambdaUpdate(SystemRoleResources.class).in(SystemRoleResources::getRoleId, idList));

        // 2、删除角色-用户关联表
        //新版角色逻辑，systemUserRole表的数据，由同步海政通的功能去维护
        //systemUserRoleService.remove(Wrappers.lambdaUpdate(SystemUserRole.class).in(SystemUserRole::getRoleId, idList));

        // 3、删除角色
        removeByIds(idList);

        // 4、递归删除孩子节点
        List<SystemRole> list = baseMapper.selectList(Wrappers.lambdaQuery(SystemRole.class).select(SystemRole::getId).in(SystemRole::getParentId, idList));
        Set<String> childrenIdList = CollUtil.set(false);
        list.forEach(item -> childrenIdList.add(item.getId()));
        removeByIdList(childrenIdList);
    }



    @Override
    public boolean checkContainBasicRole(Set<String> idList) {
        List<String> basicRole= Arrays.asList(SystemRoleConstant.TYPE_ADMIN,SystemRoleConstant.TYPE_NORMAL);
        List<SystemRole> roles=this.list(Wrappers.lambdaQuery(SystemRole.class).in(SystemRole::getId,idList).in(SystemRole::getEnName,basicRole));
        return CollectionUtils.isNotEmpty(roles);
    }

    @Override
    public String checkRoleUse(Set<String> idList) {
        List<BasicDutyOrg> basicDutyOrgs=basicDutyOrgService.list(Wrappers.lambdaQuery(BasicDutyOrg.class).in(BasicDutyOrg::getRoleIds,idList));
        if(!CollectionUtils.isEmpty(basicDutyOrgs))
        {
            return "选中的角色已绑定组织架构，不能删除";
        }
        List<PcPlanDutyOrg> planDutyOrgs=pcPlanDutyOrgService.list(Wrappers.lambdaQuery(PcPlanDutyOrg.class).in(PcPlanDutyOrg::getRoleIds,idList));
        if(!CollectionUtils.isEmpty(planDutyOrgs))
        {
            return "选中的角色已绑定预案，不能删除";
        }
        List<PcDrillDutyOrg> drillDutyOrgs=pcDrillDutyOrgService.list(Wrappers.lambdaQuery(PcDrillDutyOrg.class).in(PcDrillDutyOrg::getRoleIds,idList));
        if(!CollectionUtils.isEmpty(drillDutyOrgs))
        {
            return "选中的角色已绑定演练，不能删除";
        }
        return null;
    }
}
